<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="在线状态" prop="status">
        <el-select v-model="queryParams.status">
          <el-option
            v-for="dict in dict.type.edge_nodes_ip_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="安装状态" prop="installStatus">
        <el-select v-model="queryParams.installStatus">
          <el-option
            v-for="dict in dict.type.edge_nodes_install_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-table v-loading="loading" :data="nodesList" @selection-change="handleSelectionChange">
      <el-table-column label="节点名称" align="center" prop="nodeName" />
      <el-table-column label="IP" align="center" prop="ip" />
      <el-table-column label="DNS线路" align="center" prop="dnsLine" />
      <el-table-column label="CPU" align="center" prop="cpu" />
      <el-table-column label="内存" align="center" prop="memory" />
      <el-table-column label="下行带宽" align="center" prop="downstreamBandwidth" />
      <el-table-column label="连接" align="center" prop="connections" />
      <el-table-column prop="status" label="节点在线状态">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.edge_nodes_ip_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column prop="installStatus" label="安装状态">
        <template slot-scope="scope">
          <dict-tag
            v-if="scope.row.installStatus === 1"
            :options="dict.type.edge_nodes_install_status"
            :value="scope.row.installStatus"
            @click.native="handleClick(scope.row)"
          />
          <dict-tag
            v-else
            :options="dict.type.edge_nodes_install_status"
            :value="scope.row.installStatus"
          />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleInfo(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateStatus(scope.row)"
          >停用</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getNodeList"
    />

  </div>
</template>

<script>
import {
  DeactivatedNodeStatusByNodeId,
  deleteNodeByNodeId,
  listNodes,
} from "@/api/nodes/nodeInfo";

export default {
  dicts: ['edge_nodes_ip_status','edge_nodes_install_status'],
  name: "Clusters",
  data() {
    return {
      statusOptions:[],
      installStatusOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        installStatus: null,
        keyword: null,
        clusterId: null,
      },
      // 表单参数
      form: {
        nodeId:null,
        nodeName:'',
        ip:'',
        dnsLine:null,
        cpu:null,
        memory:null,
        downBandwidth:null,
        connections:null,
        load:null,
      },
      nodesList:[],
    };
  },
  created() {
    this.getNodeList();
  },
  methods: {
    /** 跳转安装详情 */
    handleClick(row){
      this.$router.push({ name: 'NodeInstall', params: { nodeInfo: row } }).catch(() => {});
    },
    /** 跳转节点详情*/
    handleInfo(row){
      this.$router.push({ name: 'NodeInfo', params: { nodeInfo: row } }).catch(() => {});
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getNodeList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查询根据集群Id节点列表 */
    getNodeList() {
      this.queryParams.clusterId = this.$route.params.clusterId
      this.loading = true;
      listNodes(this.queryParams).then(response => {
        this.nodesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 停用节点
    handleUpdateStatus(row) {
      this.$confirm('您确定要停用此节点吗？', '停用节点', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.loading = true;  // 开始加载
          DeactivatedNodeStatusByNodeId(row.nodeId)
            .then(response => {
              this.loading = false;  // 加载完成
              // 停用成功，显示提示消息
              this.$message({
                message: '节点停用成功',
                type: 'success',
              });
            })
            .catch(error => {
              this.loading = false;  // 加载完成
              // 错误提示
              this.$message({
                message: '节点停用失败，请重试',
                type: 'error',
              });
            });
        })
        .catch(() => {
          this.$message({
            message: '操作已取消',
            type: 'info',
          });
        });
    },

    // 删除节点
    handleDelete(row) {
      this.$confirm('您确定要删除此节点吗？', '删除节点', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error',
      })
        .then(() => {
          this.loading = true;  // 开始加载
          deleteNodeByNodeId(row.nodeId)
            .then(response => {
              this.loading = false;  // 加载完成
              // 删除成功，显示提示消息
              this.$message({
                message: '节点删除成功',
                type: 'success',
              });
              this.getNodeList();
            })
            .catch(error => {
              this.loading = false;  // 加载完成
              // 错误提示
              this.$message({
                message: '节点删除失败，请重试',
                type: 'error',
              });
            });
        })
        .catch(() => {
          this.$message({
            message: '操作已取消',
            type: 'info',
          });
        });
    }
  }
};
</script>

